Today we talked bout Chapter 19 Exceptions. 1. Exception is an object, and according to the book, whenever it occurs, "the program stops running at this point and Python prints out the traceback". Then we talked bout three main types of exceptions. 1. Logic error Feature: The program can be run, but without getting a satisfying answer. This is referred as the most terrible error since it was the algorithm that was being wrong. Example:
sumU=0for i inrange(10):
sumU+=i
print(sumU)
You want to get the sum from 1 to 10, while the code returns 45, which is the sum from 0 to 9-- it is definitely not the result you intended but it can be run. 2. Rum-time error Feature: Errors will be traced back with a message. Example:
print(1/0)
You receive a message saying "ZeroDivisionError: integer division or modulo by zero". 3. Syntax error Feature: Pycharm can highlight it for you to recognize it so it is the easiest to solve. Example:
prnt("a")
You receive a message saying "NameError: name 'print' is not defined", and the only thing you need to do is change "prnt" to the correct name "print". Use of Try Statement: It handles the exception. If no exception occurred in the try clause, the except clause will be skipped. If any exception occurred in the try clause, the except clause will be executed and then it is continued.
2. To avoid the possible errors made by users to block the program, you might want to create your own exception object and raise it. We used the example from the book:
def get_age():
age =int(input("Please enter your age: "))if age <0:
# Create a new instance of an exception
my_error =ValueError("{0} is not a valid age".format(age))raise my_error
return age
If the function that called get_age has try statement to handle this, the program continues; otherwise, the error will be traced back. To remind, the ValueError here is a built-in exception in python, instead of a random named came up to your mind. There are other types of exceptions, you can find the list here.
3. The Finally clause in try statement is used to clean up the resources we used (or close the file). Whether or not the statement in try is executed, the Finally clause will always be executed. However, only the except clause can be used to handle exceptions.
1. Exception is an object, and according to the book, whenever it occurs, "the program stops running at this point and Python prints out the traceback".
Then we talked bout three main types of exceptions.
1. Logic error
Feature: The program can be run, but without getting a satisfying answer. This is referred as the most terrible error since it was the algorithm that was being wrong.
Example:
You want to get the sum from 1 to 10, while the code returns 45, which is the sum from 0 to 9-- it is definitely not the result you intended but it can be run.
2. Rum-time error
Feature: Errors will be traced back with a message.
Example:
You receive a message saying "ZeroDivisionError: integer division or modulo by zero".
3. Syntax error
Feature: Pycharm can highlight it for you to recognize it so it is the easiest to solve.
Example:
You receive a message saying "NameError: name 'print' is not defined", and the only thing you need to do is change "prnt" to the correct name "print".
Use of Try Statement:
It handles the exception.
If no exception occurred in the try clause, the except clause will be skipped.
If any exception occurred in the try clause, the except clause will be executed and then it is continued.
2. To avoid the possible errors made by users to block the program, you might want to create your own exception object and raise it. We used the example from the book:
If the function that called get_age has try statement to handle this, the program continues; otherwise, the error will be traced back.
To remind, the ValueError here is a built-in exception in python, instead of a random named came up to your mind. There are other types of exceptions, you can find the list here.
3. The Finally clause in try statement is used to clean up the resources we used (or close the file). Whether or not the statement in try is executed, the Finally clause will always be executed. However, only the except clause can be used to handle exceptions.